<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" label-width="120px">
      <el-form-item label="志愿者手机号" prop="donateName">
        <el-input v-model="queryParams.telPhone" placeholder="请输入志愿者手机号" clearable @input="handleQueryByPhone"
          style="width: 20%" />
      </el-form-item>
      <div class="flex-row" style="width: 100%">
        <el-form-item label="兑换商品" prop="proCode" style="width: 26%;">
          <el-input v-model="queryParams.proCode" placeholder="请输入商品条码" clearable style="width: 100%;" />
        </el-form-item>
        <i class="el-icon-plus text-bold" @click="addProduct"
          style="font-size: x-large; color: #D8D8D8;line-height: 32px;margin-left: 10px;"></i>
      </div>
    </el-form>
    <div class="flex-row" style="width: 100%">
      <div style="width: 49%; margin: 0 1% 0 0">
        <!--      兑换商品-->
        <el-table v-loading="loading" :data="shopList.arExchangeItemVOs" :border="true"
          :header-cell-style="{ background: '#fff', height: '70px' }" class="customer-no-border-table shadow-sm">
          <el-table-column label="序号" type="index" align="center" width="50" />
          <el-table-column label="商品名称" align="center" prop="productName" />
          <el-table-column label="规格" align="center" prop="specification">
            <template slot-scope="scope">
              <span>{{ scope.row.specification.color }}-{{ scope.row.specification.size }}-{{
      scope.row.specification.specifications }}</span>
            </template>
          </el-table-column>
          <el-table-column label="数量" align="center" prop="productNumber">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.productNumber" :min="1" :max="scope.row.inventory"
                @change="changeNumber(scope.row, scope.$index)"></el-input-number>
              <!-- <span>{{ scope.row.inventory }}111</span> -->
            </template>
          </el-table-column>
        </el-table>

        <div style="margin: 20px 0 0" class="flex-row">
          <div class="flex-row" style="width: 35%">
            <div style="line-height: 40px;margin: 0 10px 0 0;width: auto">商品所需总积分</div>
            <div style="background: #fafbfc; height: 40px;line-height: 40px;padding: 0 10px; border: 1px solid #dbe5ee">
              {{ shopList.totalPrice }}
            </div>
          </div>
          <div class="flex-row" style="margin: 0 3% 0 3%;width: 35%">
            <div style="line-height: 40px;margin: 0 10px 0 0">志愿者可用积分</div>
            <div style="background: #fafbfc; height: 40px;line-height: 40px;padding: 0 10px; border: 1px solid #dbe5ee">
              {{ totalPoint }}
            </div>
          </div>
          <div style="width: 20%;" class="flex-row">
            <el-button icon="el-icon-refresh" size="medium " @click="resetQuery" style=" float: right;">重置</el-button>
            <el-button type="primary" size="medium " @click="exchange" style=" float: right;">确认兑换</el-button>
          </div>
        </div>
      </div>

      <!--      兑换记录-->
      <div style="width: 49%; margin: 0 0 0 1%;">
        <div
          style="background: linear-gradient(to bottom,#f7fbff,#f2f8ff); height: 70px; line-height: 70px; padding: 0 10px 0;"
          class="shadow-sm text-bold">兑换记录
        </div>

        <el-table :loading="loadingRecord" :data="exchange_record" :header-cell-style="{ background: '#fff' }"
          class="customer-no-border-table shadow-sm">
          <el-table-column prop="username" label="志愿者姓名" width="180" align="center">
          </el-table-column>
          <el-table-column prop="name" label="商品清单" width="220" align="center">
            <template slot-scope="scope">
              <div v-for="(item, index) in scope.row.arExchangeItemVOs" :key="index">
                <div style="display: inline-block;">
                  {{ item.productName }}-({{ item.specification.color }}{{ item.specification.size }}{{
      item.specification.specifications }})X {{ item.productNumber }}
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="totalPrice" label="所需积分" align="center"> </el-table-column>
          <el-table-column prop="updateTime" label="兑换时间" align="center">
          </el-table-column>
        </el-table>

      </div>
    </div>


  </div>
</template>

<script>
import { getPointByPhone, getProductByBarcode, submitExchange, listExchange } from '@/api/supermarket/scencExchange'
export default {
  name: 'SceneExchange',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        telPhone: null,
        proCode: ''
      },
      // 总积分
      totalPoint: 0,
      // 商品总积分
      totalPrice: 0,
      //兑换商品list
      shopList: {
        arExchangeItemVOs: [],
        totalPrice: 0,
        userId: ''
      },
      // // 单件商品
      // oneShopMsg: {
      //   monovalent: null,//单价
      //   proCode: null,//条形码
      //   productId: null,
      //   productName: null,
      //   productNumber: 1,//数量
      //   specification: {
      //     color: null,
      //     size: null,
      //     specifications: null
      //   },
      //   timeInterval: "",
      //   timeSlot: "",
      //   totalPrice: 0//总价
      // },
      //兑换记录
      loadingRecord: false,
      exchange_record: []
    }
  },

  created() {
    this.loading = false
    this.getList()
  },
  methods: {
    // 获取兑换记录
    getList() {
      this.loadingRecord = true
      listExchange().then(res => {
          this.exchange_record = res
          this.loadingRecord = false
      })
    },
    // 电话号码查询积分
    handleQueryByPhone() {
      if (this.queryParams.telPhone.length == 11) {
        getPointByPhone({ phone: this.queryParams.telPhone }).then(res => {
            console.log(res)
            this.totalPoint = res.point
            this.shopList.userId = res.pkId
        })
      }
    },
    resetQuery() {
      this.shopList = {
        arExchangeItemVOs: [],
        totalPrice: 0,
        userId: ''
      }
      this.queryParams= {
        telPhone: null,
        proCode: ''
      }
      this.totalPoint=0
      // 商品总积分
      this.totalPrice= 0
    },
    //增加兑换商品
    addProduct() {
      // if(this.totalPoint!==0){
      getProductByBarcode({ proCode: this.queryParams.proCode,phone: this.queryParams.telPhone }).then(res => {

          console.log(res)
        this.shopList.userId = res.userId
          this.shopList.arExchangeItemVOs.push({
            monovalent: res.integratedWorth,
            inventory: res.inventory,
            proCode: res.proCode,
            productId: res.productId,
            productName: res.productName,
            productNumber: 1,
            specification: {
              color: res.specification.color,
              size: res.specification.size,
              specifications: res.specification.specifications
            },
            timeInterval: res.timeInterval,
            timeSlot: res.timeSlot,
            totalPrice: res.integratedWorth
          })
          this.getTotalPrice()
          this.queryParams.proCode = null
          console.log(this.shopList.arExchangeItemVOs)
      }).catch(err => {
        this.queryParams.proCode=''
        // this.$message.error(err.message)
      })
      // }

    },
    // 兑换商品数量改变
    changeNumber(row, index) {
      this.shopList.arExchangeItemVOs[index].totalPrice = row.monovalent * row.productNumber
      this.getTotalPrice()
    },
    //计算总积分
    getTotalPrice() {
      let total = 0;
      this.shopList.arExchangeItemVOs.forEach(item => {
        total += item.totalPrice
      })
      this.shopList.totalPrice = total
    },
    //确认兑换
    exchange() {
      console.log(this.shopList)
      if (this.shopList.totalPrice <= this.totalPoint) {
        submitExchange(this.shopList).then(res => {
            this.$message.success('兑换成功')
            this.queryParams.telPhone = ''
            this.getList()
            this.totalPoint = 0
            this.shopList = {
              arExchangeItemVOs: [],
              totalPrice: 0,
              userId: ''
            }
        }).catch(err => {
          this.$message.error(err.message)
        })
      } else {
        this.$message.error('积分不足')
      }
    },

  }
}
</script>

<style scoped>
.customer-no-border-table.el-table--border,
.customer-no-border-table.el-table--group {
  border: none;
}
</style>
